const express = require("express");
const Ping = require("../models/PingLun")
const router = express.Router();
const Writer = require('../models/Writer');
const User = require('../models/User');
//新增评论 顶级
router.post("/addnew/:id", (req, res) => {
    //console.log(req.body);
    let newPing = new Ping({
        userid: req.params.id,
        bookid: req.body.bookid,
        writerid: req.body.writerid,
        beforeid: 0,
        text: req.body.text,
        userava: req.body.ava,
        username:req.body.name
    })
    newPing.save()
        .then(res.sendStatus(200))
})

//新增次级评论
router.post("/secHuiFu/:id", (req, res) => {
    let newPing = new Ping({
        userid: req.params.id,
        bookid: req.body.bookid,
        writerid: req.body.writerid,
        beforeid: req.body.beforeid,
        text: req.body.text,
        userava: req.body.ava,
        username: req.body.name
    })
    newPing.save()
        .then(res.sendStatus(200)) 
})

//根据书名获取顶级评论
router.post("/getTopByBookid/:id", (req, res) => {
    let a = req.body.pageSize;
    let b = req.body.currentPage
    let c = (b - 1) * a;
    Ping.find({ bookid: req.params.id, state: 1, beforeid: 0 }).skip(c).limit(a)
        .then(ping => Ping.find({ bookid: req.params.id, state: 1, beforeid: 0 }).countDocuments(true)
                .then(num => res.send({ data: ping, num:num, status: 200 }))
    )
})

//根据顶级id获取子评论
router.get("/getChildByTopId/:id", (req, res) => {
    let a = req.body.pageSize;
    let b = req.body.currentPage
    let c = (b - 1) * a;
    Ping.find({ beforeid: req.params.id, state: 1 }).skip(c).limit(a)
        .then(ping =>
            Ping.find({ beforeid: req.params.id, state: 1 }).countDocuments(true)
                .then(num => res.send({ data: ping,num:num, status: 200 }))
            )
        
})

//根据id删除评论
router.delete("/deletePing/:id", (req, res) => {
    if (req.body.beforeid == 0) {
        Ping.deleteMany({ _id: req.params.id, beforeid: req.params.id })
            .then(res.sendStatus(200))
    } else {
        Ping.findByIdAndDelete(req.params.id)
            .then(res.sendStatus(200))
    }
})

//根据书籍id获取所有评论
router.post("/allPingByBookId/:id", (req, res) => {
    let a = req.body.pageSize;
    let b = req.body.currentPage;
    let c = (b - 1) * a;
    //console.log(a);
    Ping.find({ bookid: req.params.id }, { username: 1, userid: 1, text: 1, state: 1, date: 1, beforeid: 1 }).skip(c).limit(a)
        .then(ping => {
            //console.log(ping);
            Ping.find({ bookid: req.params.id }).countDocuments(true)
                .then(num => res.send({data:ping,num:num,status:1}))
        })
        
})

// 根据作者id获取评论
router.get("/allPingByWriterId/:id", (req, res) => {
    Ping.find({ writerid: req.params.id }, { username: 1, userid: 1, tetx: 1, state: 1, date: 1, beforeid: 1 })
        .then(ping => res.send({ data: ping, status: 1 }))
})

//更改评论状态
router.post("/changePingState/:id", (req, res) => {
    if (req.body.state == 0) {
        let state = 1
        Ping.findByIdAndUpdate(req.params.id, { $set: { state: state } })
        .then(res.sendStatus(200))
    } if (req.body.state == 1) {
        let state = 0
        Ping.findByIdAndUpdate(req.params.id, { $set: { state: state } })
            .then(res.sendStatus(200))
    } 
    //console.log(1);
})

//验证评论者身份
router.get("/identity/:id", (req, res) => {
    User.findById(req.params.id)
        .then(user => {
            if (user) {
                res.send({data:1,status:200})
                return
            } else {
                Writer.findById(req.params.id)
                    .then(w => {
                        if (w) {
                            res.send({ data: 2, status: 200 })
                            return
                        } else {
                            res.send({data:404})
                        }
                    }

                    )
            }
        })
})
module.exports = router